package org.zoomdev.zoom;

import jakarta.servlet.DispatcherType;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.MultipartConfigElement;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.Part;
import org.eclipse.jetty.ee10.servlet.FilterHolder;
import org.eclipse.jetty.ee10.webapp.WebAppContext;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.thread.VirtualThreadPool;
import org.zoomdev.zoom.common.io.Io;
import org.zoomdev.zoom.common.utils.PathUtils;

import java.io.IOException;
import java.util.EnumSet;

/**
 * Hello world!
 */

public class App {
    public static void main(String[] args) throws Exception {
        new App().test1();
    }




    //    @Test
    public void test1() throws Exception {
        // 1. 创建 Jetty 服务器实例，监听 8080 端口
        Server server = new Server(new VirtualThreadPool());
// 创建连接器并指定端口
        ServerConnector connector = new ServerConnector(server);
        connector.setPort(8080);  // 明确指定监听8080端口
        server.addConnector(connector);  // 将连接器附加到服务器


        WebAppContext webAppContext = new WebAppContext();
        webAppContext.setContextPath("/");
        webAppContext.setBaseResourceAsString(PathUtils.getRootPath());
        webAppContext.addFilter(new FilterHolder(new Filter() {
            @Override
            public void doFilter(ServletRequest request, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
                try{
                    HttpServletRequest hq = (HttpServletRequest) request;
                    MultipartConfigElement multipartConfig = new MultipartConfigElement(
                            "d:\\tmp",      // 临时存储目录
                            50_000_000,  // maxRequestSize (50MB)
                            50_000_000,  // maxFileSize
                            10_000_000   // fileSizeThreshold
                    );
                    request.setAttribute(
                            "org.eclipse.jetty.multipartConfig",
                            multipartConfig
                    );

                    System.out.println(hq.getContextPath());
                    System.out.println(hq.getContentType());
                    System.out.println(hq.getContentLength());


                    Part part = hq.getPart("file");
                    byte[] bytes = Io.readBytes(part.getInputStream());
                    System.out.println(bytes.length);
                    servletResponse.getWriter().print("hello world");
                }catch (IOException e){
                    e.printStackTrace();
                    throw e;
                }catch (ServletException e){
                    e.printStackTrace();
                    throw e;
                }

            }

            @Override
            public void init(FilterConfig filterConfig) throws ServletException {



            }
        }), "/*", EnumSet.of(DispatcherType.REQUEST));
        // 4. 将上下文处理器绑定到服务器
        webAppContext.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");
        webAppContext.setInitParameter("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "false");

        server.setHandler(webAppContext);
        // 5. 启动服务器
        server.start();
        server.join();
    }


}
